// Copyright 2015-2019 Elviss Strazdins. All rights reserved.

#include "SpritesSample.hpp"
#include "MainMenu.hpp"

using namespace ouzel;
using namespace input;

SpritesSample::SpritesSample():
    hideButton("button.png", "button_selected.png", "button_down.png", "", "Show/hide", "Arial", 1.0F, Color::black(), Color::black(), Color::black()),
    wireframeButton("button.png", "button_selected.png", "button_down.png", "", "Wireframe", "Arial", 1.0F, Color::black(), Color::black(), Color::black()),
    backButton("button.png", "button_selected.png", "button_down.png", "", "Back", "Arial", 1.0F, Color::black(), Color::black(), Color::black())
{
    handler.gamepadHandler = [](const GamepadEvent& event) {
        if (event.type == Event::Type::GamepadButtonChange)
        {
            if (event.pressed &&
                event.button == Gamepad::Button::FaceRight)
                engine->getSceneManager().setScene(std::make_unique<MainMenu>());
        }

        return false;
    };

    handler.uiHandler = [this](const UIEvent& event) {
        if (event.type == Event::Type::ActorClick)
        {
            if (event.actor == &backButton)
                engine->getSceneManager().setScene(std::make_unique<MainMenu>());
            else if (event.actor == &hideButton)
                character.setHidden(!character.isHidden());
            else if (event.actor == &wireframeButton)
                camera.setWireframe(!camera.getWireframe());
        }

        return false;
    };

    handler.keyboardHandler = [](const KeyboardEvent& event) {
        if (event.type == Event::Type::KeyboardKeyPress)
        {
            switch (event.key)
            {
                case Keyboard::Key::Escape:
                case Keyboard::Key::Menu:
                case Keyboard::Key::Back:
                    engine->getSceneManager().setScene(std::make_unique<MainMenu>());
                    return true;
                default:
                    break;
            }
        }
        else if (event.type == Event::Type::KeyboardKeyRelease)
        {
            switch (event.key)
            {
                case Keyboard::Key::Escape:
                case Keyboard::Key::Menu:
                case Keyboard::Key::Back:
                    return true;
                default:
                    break;
            }
        }

        return false;
    };

    engine->getEventDispatcher().addEventHandler(handler);

    camera.setClearColorBuffer(true);
    camera.setClearColor(ouzel::Color(64, 64, 64));
    camera.setScaleMode(scene::Camera::ScaleMode::ShowAll);
    camera.setTargetContentSize(Size2F(800.0F, 600.0F));
    cameraActor.addComponent(&camera);
    layer.addChild(&cameraActor);
    addLayer(&layer);

    // character
    characterSprite.init("run.json");
    characterSprite.setAnimation("", true);
    characterSprite.play();

    character.addComponent(&characterSprite);
    layer.addChild(&character);
    character.setPosition(Vector2F(-300.0F, 0.0F));

    move = std::make_unique<scene::Move>(4.0F, Vector3F(300.0F, 0.0F, 0.0F));
    character.addComponent(move.get());
    move->start();

    // fire
    fireSprite.init("fire.json");
    fireSprite.setOffset(Vector2F(0.0F, 20.0F));
    fireSprite.setAnimation("", true);
    fireSprite.play();

    fireActor.addComponent(&fireSprite);
    fireActor.setPosition(Vector2F(-100.0F, -140.0F));
    layer.addChild(&fireActor);

    // triangle
    triangleSprite.init("triangle.json");

    triangleActor.addComponent(&triangleSprite);
    triangleActor.setPosition(Vector2F(100.0F, -140.0F));
    layer.addChild(&triangleActor);

    guiCamera.setScaleMode(scene::Camera::ScaleMode::ShowAll);
    guiCamera.setTargetContentSize(Size2F(800.0F, 600.0F));
    guiCameraActor.addComponent(&guiCamera);
    guiLayer.addChild(&guiCameraActor);
    addLayer(&guiLayer);

    guiLayer.addChild(&menu);

    hideButton.setPosition(Vector2F(-200.0F, 200.0F));
    menu.addWidget(&hideButton);

    wireframeButton.setPosition(Vector2F(-200.0F, 160.0F));
    menu.addWidget(&wireframeButton);

    backButton.setPosition(Vector2F(-200.0F, -200.0F));
    menu.addWidget(&backButton);
}
